package com.kjtpay.design_pattern.observer;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.kjtpay.design_pattern.R;
import com.kjtpay.design_pattern.builder.BuilderActivity;

public class ObserverActivity extends AppCompatActivity {

    public static void launch(Context context) {
        if (null != context) {
            Intent intent = new Intent();
            intent.setClass(context, ObserverActivity.class);
            if (!(context instanceof Activity)) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            context.startActivity(intent);
        }
    }

    TextView whatToDoTextView;
    Subject whoComeSubject;
    StockObserver notObserver;
    StockObserver observer;
    NBAObserver nbaObserver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_observer);
        setTitle("观察者模式");

        whatToDoTextView = findViewById(R.id.textViewWhatToDo);
        whoComeSubject = new Subject();
        notObserver = new StockObserver("魏光茶");
        observer = new StockObserver("易冠茶");
        nbaObserver = new NBAObserver("朗球迷");
        // notObserver不加入观察列表
        whoComeSubject.attach(observer);
        whoComeSubject.attach(nbaObserver);
    }

    public void onBossClick(View view) {
        whoComeSubject.setSubjectState("老板");
        whoComeSubject.notice();
    }

    public void onSecretaryClick(View view) {
        whoComeSubject.setSubjectState("秘书");
        whoComeSubject.notice();
    }

    public void onNotObserverClick(View view) {
        whatToDoTextView.setText(notObserver.showAction());
    }

    public void onObserverClick(View view) {
        whatToDoTextView.setText(observer.showAction());
    }

    public void onNBAClick(View view) {
        whatToDoTextView.setText(nbaObserver.showAction());
    }
}